package com.lovepick.map;

import android.location.Location;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;

/**
 * @author liutong
 */
public class MapHelper implements MKSearchListener
{
	private static final String TAG = "MapHelper";
	public static final String DRIVE_RESULT = "drive_result";
	public static final String POI_INFOS = "poi_infos";
	public static final String ADDR_INFO = "addr_info";

	public static final int DRIVE_COMPLETE = 0;
	public static final int POI_COMPLETE = 1;
	public static final int ADDR_COMPLETE = 2;

	private BMapManager mBMapManager;
	private MKLocationManager mLocationManager = null;
	private Handler mHandler;
	private MKSearch mMKSearch = null;

	private MKAddrInfo mAddrInfo = null;
	private MKDrivingRouteResult mRouteResult = null;
	private MKPoiResult mPoiResult = null;

	public MKAddrInfo getAddrInfo()
	{
		return mAddrInfo;
	}

	public MKDrivingRouteResult getDrivingRouteResult()
	{
		return mRouteResult;
	}

	public MKPoiResult getPoiResult()
	{
		return mPoiResult;
	}

	public MapHelper(BMapManager mapManager, Handler handler)
	{
		this.mBMapManager = mapManager;
		this.mHandler = handler;
	}

	private LocationListener locationListener = new LocationListener()
	{

		@Override
		public void onLocationChanged(Location location)
		{
			Log.i(TAG, "---location changed---");
			if (location != null)
			{
				// TODO Auto-generated method stub
				Log.i(TAG, location.toString());
				int latitudeE6 = (int) (location.getLatitude() * 1E6);
				Log.i(TAG, "latitude:" + String.valueOf(latitudeE6));
				int longitudeE6 = (int) (location.getLongitude() * 1E6);
				Log.i(TAG, "longitude:" + String.valueOf(longitudeE6));
				GeoPoint geoPoint = new GeoPoint(latitudeE6, longitudeE6);
				getAddrFromGeo(geoPoint);
			}
		}
	};

	public void getCurrentLocation()
	{
		Log.i(TAG, "---getCurrentLocation---");
		this.mLocationManager = mBMapManager.getLocationManager();
		mLocationManager.enableProvider(MKLocationManager.MK_GPS_PROVIDER);
		mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);

		mLocationManager.requestLocationUpdates(locationListener);
	}

	/**
	 * 使用百度地图搜索驾车路线, 并在地图上展示其中的一条路线.
	 * 
	 * @param startPoint	起点
	 * @param endPoint		 终点
	 */
	public void searchDrivingRoute(GeoPoint startPoint, GeoPoint endPoint)
	{
		Log.i(TAG, "-----searchDrivingRoute-----");
		MKPlanNode start = new MKPlanNode();
		start.pt = startPoint;
		MKPlanNode end = new MKPlanNode();
		end.pt = endPoint;

		mMKSearch = new MKSearch();
		mMKSearch.init(mBMapManager, this);
		// 驾车路线搜索策略, 时间优先
		mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
		Log.i(TAG, "开始搜索驾车路线 ");
		mMKSearch.drivingSearch(null, start, null, end);
	}

	public void searchPoi(String city, String key)
	{
		mMKSearch = new MKSearch();
		mMKSearch.init(mBMapManager, this);
		Log.i(TAG, "##poi search start##");
		mMKSearch.poiSearchInCity(city, key);
	}

	public void getAddrFromGeo(GeoPoint point)
	{
		mMKSearch = new MKSearch();
		mMKSearch.init(mBMapManager, this);
		Log.i(TAG, "reverse geocode start...");
		mMKSearch.reverseGeocode(point);
	}

	@Override
	public void onGetAddrResult(MKAddrInfo result, int error)
	{
		// TODO Auto-generated method stub
		Log.i(TAG, "AddrINfo error:" + error);

		if (result == null)
		{
			Log.i(TAG, "AddrInfo: no result");
		}

		mLocationManager.removeUpdates(locationListener);
		this.mAddrInfo = result;
		Message msg = new Message();
		msg.what = ADDR_COMPLETE;
		mHandler.sendMessage(msg);
	}

	@Override
	public void onGetBusDetailResult(MKBusLineResult result, int error)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetDrivingRouteResult(MKDrivingRouteResult result, int error)
	{
		// TODO Auto-generated method stub
		Log.i(TAG,
				"MKDrivingRouteResult error: " + error);
		if (result == null)
		{
			Log.i(TAG, "MKDrivingRouteResult: no result");
		}
		this.mRouteResult = result;
		Message msg = new Message();
		msg.what = DRIVE_COMPLETE;
		mHandler.sendMessage(msg);
	}

	@Override
	public void onGetPoiResult(MKPoiResult result, int type, int error)
	{
		// TODO Auto-generated method stub
		Log.i(TAG, "MKPoiResult error:" + error);
		Log.i(TAG, "MKPoiResult type:" + type);

		this.mPoiResult = result;
		Message msg = new Message();
		msg.what = POI_COMPLETE;
		mHandler.sendMessage(msg);

	}

	@Override
	public void onGetSuggestionResult(MKSuggestionResult result, int error)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetTransitRouteResult(MKTransitRouteResult result, int error)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetWalkingRouteResult(MKWalkingRouteResult result, int error)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetRGCShareUrlResult(String arg0, int arg1)
	{
		// TODO Auto-generated method stub

	}
}
